package com.shujia.flink.core

import org.apache.flink.api.scala._
import org.apache.flink.core.fs.FileSystem.WriteMode

object Demo1BatchWordCount {
  def main(args: Array[String]): Unit = {

    /**
      * 创建flink的运价环境
      * ExecutionEnvironment  批处理环境
      */

    val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment


    //设置并行度
    env.setParallelism(1)

    //1、读取数据
    val linesDS: DataSet[String] = env.readTextFile("flink/data/words.txt")


    //2、统计单词的数量
    val countDS: DataSet[String] = linesDS.flatMap(_.split(","))
      .map(word => (word, 1))
      .groupBy(0)
      .sum(1)
      .map(kv => s"${kv._1},${kv._2}")


    countDS.writeAsText("flink/data/count", WriteMode.OVERWRITE)


    //启动flink程序
    env.execute()

  }
}
